package com.ruoyi.project.men.api;


import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bootdo.R;
import com.ruoyi.project.men.domain.MenNeedTypeDO;
import com.ruoyi.project.men.service.MenNeedInfoService;
import com.ruoyi.project.men.service.MenNeedTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/need")
public class ApiNeedController {
    @Autowired
    private MenNeedTypeService menNeedTypeService;
    @Autowired
    private MenNeedInfoService menNeedInfoService;

    /***
     * 获取需求类型和需求列表
     * @param params
     * @return
     */
    @PostMapping( "/getNeedListAndType")
    @ResponseBody
    public R getNeedListAndType(@RequestBody Map<String, Object> params){

        try{

            return menNeedInfoService.getNeedListAndType(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     *  获取需求类型
     * @param
     * @return
     */
    @PostMapping( "/getNeedType")
    @ResponseBody
    public R getNeedType(){

        try{
            Map<String,Object> map = new HashMap<>();
            map.put("delFlag","0");
            map.put("sort","ca_at");
            map.put("order","desc");
            List<MenNeedTypeDO> list = menNeedTypeService.list(map);
            return R.ok().put("data",list);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }

    /***
     *
     * @param `typeId` 类型id
     * @return
     */
    @PostMapping( "/getNeedListByTypeId")
    @ResponseBody
    public R getNeedListByTypeId(@RequestBody Map<String, Object> params){

        try{
            if (!params.containsKey("typeId") || StringUtils.isBlank(params.get("typeId").toString())

            ) {
                return R.error("参数错误");
            }
            return menNeedInfoService.getNeedListByTypeId(params);
        }catch (Exception e){
            return R.error("服务器网络异常");
        }
    }
}
